package practise.hibernate.interceptor;


import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.hibernate.CallbackException;
import org.hibernate.EmptyInterceptor;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.type.Type;

import javax.persistence.*;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.*;

public class LogInterceptor extends EmptyInterceptor {
    Logger logger = Logger.getLogger(LogInterceptor.class);

    @Override
    public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
        logger.log(Priority.INFO , "---- on delete entity :"+entity+" properties :"+ propertyNames + "types:"+types);
        super.onDelete(entity, id, state, propertyNames, types);    //To change body of overridden methods use File | Settings | File Templates.
       // processCascadeRemove(entity);

    }

    @Override
    public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
        logger.log(Priority.INFO , "---- on onFlushDirty , entity: "+entity +"previsou state : "+previousState + "current state : "+ currentState  );
        return super.onFlushDirty(entity, id, currentState, previousState, propertyNames, types);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
        logger.log(Priority.INFO , "---- onSave  entity :"+entity+" properties :"+ propertyNames + "types:"+types);
        return super.onSave(entity, id, state, propertyNames, types);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public void postFlush(Iterator entities) {
        while (entities.hasNext()){
            logger.log(Priority.INFO , "---- postFlush entity:" + entities);
            entities.next();
        }
        super.postFlush(entities);    //To change body of overridden methods use File | Settings | File Templates.

    }

    @Override
    public void preFlush(Iterator entities) {
        while (entities.hasNext()){
            logger.log(Priority.INFO , "---- preFlush entity:" + entities);
            entities.next();
        }
        super.preFlush(entities);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public int[] findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
        return super.findDirty(entity, id, currentState, previousState, propertyNames, types);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public void onCollectionRemove(Object collection, Serializable key) throws CallbackException {
        logger.log(Priority.INFO , "----onCollectionRemove entity:" + collection + " key:" + key);
        super.onCollectionRemove(collection, key);
    }

    @Override
    public void onCollectionRecreate(Object collection, Serializable key) throws CallbackException {
        logger.log(Priority.INFO , "----onCollectionRecreate entity:" + collection + " key:" + key);
        super.onCollectionRecreate(collection, key);
    }

    @Override
    public void onCollectionUpdate(Object collection, Serializable key) throws CallbackException {
        logger.log(Priority.INFO , "----onCollectionUpdate entity:" + collection + " key:" + key);
        super.onCollectionUpdate(collection, key);
    }


}
